package com.h3bpm.kumhosunny.crm.controller;

import OThinker.H3.Controller.ControllerBase;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataTable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @authormi
 * @date2019/12/16
 */
@Controller
@RequestMapping({"/Portal/CrmSum"})
public class CrmSumController extends ControllerBase {
    @Override
    public String getFunctionCode() {
        return null;
    }


    @RequestMapping("/getCrmSumData")
    @ResponseBody
    public JSONObject getCrmSumData(){



     String khSumSql = "select count(1) as khNum from i_CustomerProfile ";

     String lxrSumSql = "select count(1) as lxrNum from i_ContactFileManager";

     String sjSumSql = "select count(1) as sjNum from i_businessOpportunity";

     String ccSumSql = "select count(1) as ccNum from i_businessTravelReport a  inner join ot_instancecontext b on b.BizObjectId=a.ObjectID where (b.State=2 or  b.State=4)";

     String thisUserObj = getUserValidator().getUser().getObjectID();

    String thisKhSumSql = "select count(1) as khNum from i_CustomerProfile where salesman='"+thisUserObj+"'";

    String thisLxrSumSql = "select count(1) as lxrNum from i_ContactFileManager where exclusiveSalesman='"+thisUserObj+"'";

    String thisSjSumSql = "select count(1) as sjNum from i_businessOpportunity where CreatedBy='"+thisUserObj+"'";

    String thisCcSumSql = "select count(1) as ccNum from i_businessTravelReport a  inner join ot_instancecontext b on b.BizObjectId=a.ObjectID where (b.State=2 or  b.State=4)" +
            " and   b.Originator= '"+thisUserObj+"'";




        JSONObject jsonResult =new JSONObject();

        try {
          DataTable khDataTable  = SqlUtil.doQuery(khSumSql);
          DataTable lxrDataTable = SqlUtil.doQuery(lxrSumSql);
          DataTable sjDataTable  = SqlUtil.doQuery(sjSumSql);
          DataTable ccDataTable  = SqlUtil.doQuery(ccSumSql);


        DataTable thisKhDataTable  = SqlUtil.doQuery(thisKhSumSql);
        DataTable thisLxrDataTable = SqlUtil.doQuery(thisLxrSumSql);
        DataTable thisSjDataTable  = SqlUtil.doQuery(thisSjSumSql);
        DataTable thisCcDataTable  = SqlUtil.doQuery(thisCcSumSql);

          JSONArray jsonArray =new JSONArray();
          JSONObject khJson= new JSONObject();
          JSONObject lxrJson= new JSONObject();
          JSONObject sjJson= new JSONObject();
          JSONObject ccJson= new JSONObject();

            khJson.put("name","客户统计");
            khJson.put("Num",khDataTable.getEntityRows().get(0).getString("khNum"));
            khJson.put("thisNum",thisKhDataTable.getEntityRows().get(0).getString("khNum"));
            lxrJson.put("name","客户联系人统计");
            lxrJson.put("Num",lxrDataTable.getEntityRows().get(0).getString("lxrNum"));
            lxrJson.put("thisNum",thisLxrDataTable.getEntityRows().get(0).getString("lxrNum"));
            sjJson.put("name","商机统计");
            sjJson.put("Num",sjDataTable.getEntityRows().get(0).getString("sjNum"));
            sjJson.put("thisNum",thisSjDataTable.getEntityRows().get(0).getString("sjNum"));
            ccJson.put("name","出差统计");
            ccJson.put("Num",ccDataTable.getEntityRows().get(0).getString("ccNum"));
            ccJson.put("thisNum",thisCcDataTable.getEntityRows().get(0).getString("ccNum"));

            jsonArray.add(khJson);
            jsonArray.add(lxrJson);
            jsonArray.add(sjJson);
            jsonArray.add(ccJson);
            jsonResult.put("code",200);
            jsonResult.put("status","200");
            jsonResult.put("data",jsonArray);

            jsonResult.put("errorMsg","请求成功");

            return jsonResult;
        } catch (Exception e) {
            jsonResult.put("status","500");
            e.printStackTrace();
        }
        return jsonResult;
    }




}
